package com.huawei.hianalytics.log.f;

import android.content.Context;
import android.text.TextUtils;
import com.huawei.hianalytics.log.a.a;
import com.huawei.hianalytics.log.e.c;
import com.huawei.hianalytics.log.e.d;
import com.huawei.hianalytics.log.e.e;
import com.huawei.hianalytics.log.e.f;
import com.huawei.hianalytics.util.h;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.Key;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import org.json.JSONException;
import org.json.JSONObject;

public class b implements c {
    private String a(String str) {
        return "https://" + str;
    }

    private static void a(ZipInputStream zipInputStream, File file) {
        Closeable fileOutputStream;
        Throwable th;
        Throwable th2;
        Closeable closeable = null;
        try {
            ZipEntry nextEntry = zipInputStream.getNextEntry();
            if (nextEntry == null) {
                d.a(0, null);
                return;
            }
            Object c = c(nextEntry.getName());
            if (TextUtils.isEmpty(c)) {
                com.huawei.hianalytics.g.b.c("SendManagerImpl", "File name exception, stop unzip");
                d.a(0, null);
                return;
            }
            File file2 = new File(file.getCanonicalPath() + File.separator + c);
            if (nextEntry.isDirectory()) {
                if (file2.mkdirs()) {
                    a(zipInputStream, file);
                } else {
                    com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "fileUnZip() Unzip file creation failure");
                }
            } else if (file2.createNewFile()) {
                fileOutputStream = new FileOutputStream(file2);
                try {
                    byte[] bArr = new byte[2048];
                    int i = 0;
                    while (true) {
                        int read = zipInputStream.read(bArr);
                        if (read == -1) {
                            break;
                        }
                        i += read;
                        if (!a(i, file2)) {
                            break;
                        }
                        fileOutputStream.write(bArr, 0, read);
                    }
                    a(zipInputStream, file);
                    closeable = fileOutputStream;
                } catch (IOException e) {
                    closeable = fileOutputStream;
                    try {
                        com.huawei.hianalytics.g.b.d("SendManagerImpl", "fileUnZip() File creation failure or Stream Exception!");
                        d.a(0, closeable);
                    } catch (Throwable th3) {
                        th = th3;
                        fileOutputStream = closeable;
                        th2 = th;
                        d.a(0, fileOutputStream);
                        throw th2;
                    }
                } catch (Throwable th4) {
                    th2 = th4;
                    d.a(0, fileOutputStream);
                    throw th2;
                }
            } else {
                com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "fileUnZip() Failure to create new files");
            }
            d.a(0, closeable);
        } catch (IOException e2) {
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "fileUnZip() File creation failure or Stream Exception!");
            d.a(0, closeable);
        } catch (Throwable th32) {
            th = th32;
            fileOutputStream = null;
            th2 = th;
            d.a(0, fileOutputStream);
            throw th2;
        }
    }

    private static boolean a(int i, File file) {
        if (i <= 5242880) {
            return true;
        }
        com.huawei.hianalytics.g.b.d("SendManagerImpl", "Single File being unzipped is too big.");
        if (file.exists() && file.delete()) {
            com.huawei.hianalytics.g.b.b("SendManagerImpl", "Delete large files successfully");
        }
        return false;
    }

    private String b(String str, String str2, String str3) {
        return a.a(str, str2, str3);
    }

    private void b(String str) {
        String[] split = str.split("/");
        if ("logs".equals(split[split.length - 2])) {
            a(str.replace(split[split.length - 1], "logzips"), str);
        } else if (new File(str).delete()) {
            com.huawei.hianalytics.g.b.c("SendManagerImpl", "doUnzip() delete srcFilePath file");
        }
    }

    private static String c(String str) {
        try {
            String canonicalPath = new File(str).getCanonicalPath();
            if (canonicalPath.startsWith(new File(".").getCanonicalPath())) {
                return canonicalPath;
            }
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "File is outside extraction target directory.");
            return "";
        } catch (IOException e) {
            return "";
        }
    }

    public void a(String str, String str2) {
        ZipFile zipFile;
        Closeable zipInputStream;
        Closeable closeable;
        Throwable th;
        Throwable th2;
        ZipFile zipFile2 = null;
        try {
            zipFile = new ZipFile(str2);
            try {
                if (zipFile.size() > 10) {
                    com.huawei.hianalytics.g.b.c("SendManagerImpl", "The number of unzip files is too much.MaxSize: 10");
                    d.a(4, null);
                    if (zipFile != null) {
                        try {
                            zipFile.close();
                            return;
                        } catch (IOException e) {
                            com.huawei.hianalytics.g.b.c("SendManagerImpl", "ZipFile. Exception when closing the closeable");
                            return;
                        }
                    }
                    return;
                }
                zipInputStream = new ZipInputStream(new FileInputStream(str2));
                try {
                    File file = new File(str);
                    if (!(file.exists() || file.mkdirs())) {
                        com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "Zips directory creation failure");
                    }
                    a((ZipInputStream) zipInputStream, file);
                    if (new File(str2).delete()) {
                        com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "Delete unzip file");
                    }
                    d.a(4, zipInputStream);
                    if (zipFile != null) {
                        try {
                            zipFile.close();
                        } catch (IOException e2) {
                            com.huawei.hianalytics.g.b.c("SendManagerImpl", "ZipFile. Exception when closing the closeable");
                        }
                    }
                } catch (FileNotFoundException e3) {
                    closeable = zipInputStream;
                    try {
                        com.huawei.hianalytics.g.b.d("SendManagerImpl", " unZip() There is no document!");
                        d.a(4, closeable);
                        if (zipFile == null) {
                            try {
                                zipFile.close();
                            } catch (IOException e4) {
                                com.huawei.hianalytics.g.b.c("SendManagerImpl", "ZipFile. Exception when closing the closeable");
                            }
                        }
                    } catch (Throwable th3) {
                        th = th3;
                        zipInputStream = closeable;
                        zipFile2 = zipFile;
                        th2 = th;
                        d.a(4, zipInputStream);
                        if (zipFile2 != null) {
                            try {
                                zipFile2.close();
                            } catch (IOException e5) {
                                com.huawei.hianalytics.g.b.c("SendManagerImpl", "ZipFile. Exception when closing the closeable");
                            }
                        }
                        throw th2;
                    }
                } catch (IOException e6) {
                    try {
                        com.huawei.hianalytics.g.b.d("SendManagerImpl", "This file is not a compressed file");
                        d.a(4, zipInputStream);
                        if (zipFile == null) {
                            try {
                                zipFile.close();
                            } catch (IOException e7) {
                                com.huawei.hianalytics.g.b.c("SendManagerImpl", "ZipFile. Exception when closing the closeable");
                            }
                        }
                    } catch (Throwable th4) {
                        th = th4;
                        zipFile2 = zipFile;
                        th2 = th;
                        d.a(4, zipInputStream);
                        if (zipFile2 != null) {
                            zipFile2.close();
                        }
                        throw th2;
                    }
                }
            } catch (FileNotFoundException e8) {
                com.huawei.hianalytics.g.b.d("SendManagerImpl", " unZip() There is no document!");
                d.a(4, closeable);
                if (zipFile == null) {
                    zipFile.close();
                }
            } catch (IOException e9) {
                zipInputStream = null;
                com.huawei.hianalytics.g.b.d("SendManagerImpl", "This file is not a compressed file");
                d.a(4, zipInputStream);
                if (zipFile == null) {
                    zipFile.close();
                }
            } catch (Throwable th32) {
                th = th32;
                zipInputStream = null;
                zipFile2 = zipFile;
                th2 = th;
                d.a(4, zipInputStream);
                if (zipFile2 != null) {
                    zipFile2.close();
                }
                throw th2;
            }
        } catch (FileNotFoundException e10) {
            zipFile = null;
            com.huawei.hianalytics.g.b.d("SendManagerImpl", " unZip() There is no document!");
            d.a(4, closeable);
            if (zipFile == null) {
                zipFile.close();
            }
        } catch (IOException e11) {
            zipFile = null;
            zipInputStream = null;
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "This file is not a compressed file");
            d.a(4, zipInputStream);
            if (zipFile == null) {
                zipFile.close();
            }
        } catch (Throwable th5) {
            th2 = th5;
            zipInputStream = null;
            d.a(4, zipInputStream);
            if (zipFile2 != null) {
                zipFile2.close();
            }
            throw th2;
        }
    }

    public void a(String str, String str2, Key key) {
        Closeable fileInputStream;
        Closeable fileOutputStream;
        Throwable th;
        Throwable th2;
        Closeable closeable = null;
        byte[] a = com.huawei.hianalytics.log.e.b.a();
        Cipher a2 = com.huawei.hianalytics.log.e.b.a(1, key, a);
        if (a2 == null) {
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "get cipher is null!");
            b(str);
            return;
        }
        try {
            fileInputStream = new FileInputStream(str);
            try {
                fileOutputStream = new FileOutputStream(str2);
            } catch (FileNotFoundException e) {
                fileOutputStream = null;
                closeable = fileInputStream;
                try {
                    com.huawei.hianalytics.g.b.d("SendManagerImpl", " encrypt(): There is no document!");
                    d.a(1, closeable);
                    d.a(0, fileOutputStream);
                } catch (Throwable th3) {
                    th = th3;
                    fileInputStream = closeable;
                    closeable = fileOutputStream;
                    th2 = th;
                    d.a(1, fileInputStream);
                    d.a(0, closeable);
                    throw th2;
                }
            } catch (BadPaddingException e2) {
                fileOutputStream = null;
                try {
                    com.huawei.hianalytics.g.b.d("SendManagerImpl", " encrypt(): diFinal - False filling parameters!");
                    b(str);
                    d.a(1, fileInputStream);
                    d.a(0, fileOutputStream);
                } catch (Throwable th4) {
                    th = th4;
                    closeable = fileOutputStream;
                    th2 = th;
                    d.a(1, fileInputStream);
                    d.a(0, closeable);
                    throw th2;
                }
            } catch (IllegalBlockSizeException e3) {
                fileOutputStream = null;
                com.huawei.hianalytics.g.b.d("SendManagerImpl", "encrypt(): doFinal - The provided block is not filled with !");
                b(str);
                d.a(1, fileInputStream);
                d.a(0, fileOutputStream);
            } catch (IOException e4) {
                fileOutputStream = null;
                com.huawei.hianalytics.g.b.d("SendManagerImpl", "Exception by stream read or write in the encrypt()!");
                b(str);
                d.a(1, fileInputStream);
                d.a(0, fileOutputStream);
            } catch (Throwable th5) {
                th2 = th5;
                d.a(1, fileInputStream);
                d.a(0, closeable);
                throw th2;
            }
            try {
                fileOutputStream.write(a);
                byte[] bArr = new byte[2048];
                while (fileInputStream.read(bArr) != -1) {
                    fileOutputStream.write(a2.doFinal(bArr));
                }
                d.a(1, fileInputStream);
                d.a(0, fileOutputStream);
            } catch (FileNotFoundException e5) {
                closeable = fileInputStream;
                com.huawei.hianalytics.g.b.d("SendManagerImpl", " encrypt(): There is no document!");
                d.a(1, closeable);
                d.a(0, fileOutputStream);
            } catch (BadPaddingException e6) {
                com.huawei.hianalytics.g.b.d("SendManagerImpl", " encrypt(): diFinal - False filling parameters!");
                b(str);
                d.a(1, fileInputStream);
                d.a(0, fileOutputStream);
            } catch (IllegalBlockSizeException e7) {
                com.huawei.hianalytics.g.b.d("SendManagerImpl", "encrypt(): doFinal - The provided block is not filled with !");
                b(str);
                d.a(1, fileInputStream);
                d.a(0, fileOutputStream);
            } catch (IOException e8) {
                com.huawei.hianalytics.g.b.d("SendManagerImpl", "Exception by stream read or write in the encrypt()!");
                b(str);
                d.a(1, fileInputStream);
                d.a(0, fileOutputStream);
            }
        } catch (FileNotFoundException e9) {
            fileOutputStream = null;
            com.huawei.hianalytics.g.b.d("SendManagerImpl", " encrypt(): There is no document!");
            d.a(1, closeable);
            d.a(0, fileOutputStream);
        } catch (BadPaddingException e10) {
            fileOutputStream = null;
            fileInputStream = null;
            com.huawei.hianalytics.g.b.d("SendManagerImpl", " encrypt(): diFinal - False filling parameters!");
            b(str);
            d.a(1, fileInputStream);
            d.a(0, fileOutputStream);
        } catch (IllegalBlockSizeException e11) {
            fileOutputStream = null;
            fileInputStream = null;
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "encrypt(): doFinal - The provided block is not filled with !");
            b(str);
            d.a(1, fileInputStream);
            d.a(0, fileOutputStream);
        } catch (IOException e12) {
            fileOutputStream = null;
            fileInputStream = null;
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "Exception by stream read or write in the encrypt()!");
            b(str);
            d.a(1, fileInputStream);
            d.a(0, fileOutputStream);
        } catch (Throwable th6) {
            th2 = th6;
            fileInputStream = null;
            d.a(1, fileInputStream);
            d.a(0, closeable);
            throw th2;
        }
    }

    public boolean a(String str, String str2, Context context) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2) || context == null) {
            com.huawei.hianalytics.g.b.c("SendManagerImpl", "send log path or key is empty!");
            return false;
        }
        String str3 = "";
        str3 = "";
        str3 = "";
        Object c = com.huawei.hianalytics.a.d.c();
        String b = com.huawei.hianalytics.a.d.b();
        if (TextUtils.isEmpty(b) || TextUtils.isEmpty(c)) {
            com.huawei.hianalytics.g.b.c("SendManagerImpl", "logClintID or logClintKey is empty! Do not send file!");
            return false;
        }
        e fVar = new f();
        String concat = "{url}/v2/getServerDomain".replace("{url}", com.huawei.hianalytics.a.d.i()).concat("?appID=").concat(b);
        String a = e.a(context);
        try {
            JSONObject jSONObject = new JSONObject(com.huawei.hianalytics.h.b.a(concat, a, b(concat, a, c)).b());
            str3 = jSONObject.getString("resCode");
            com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "logserver first request resCode :" + str3);
            if (!"0".equals(str3)) {
                return false;
            }
            str3 = jSONObject.optString("serverDomain", "");
            concat = jSONObject.optString("accessToken", "");
            CharSequence a2 = a(str3);
            if (h.a(a2)) {
                String concat2 = "{url}/v2/getUploadInfo".replace("{url}", a2).concat("?appID=").concat(b);
                String a3 = fVar.a(a, fVar.a(str, str2));
                JSONObject jSONObject2 = new JSONObject(com.huawei.hianalytics.h.b.a(concat2, a3, a.a(concat2, a3, concat)).b());
                concat2 = jSONObject2.getString("resCode");
                com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "Request file to report URL interface serverResCode :" + concat2);
                if (!"0".equals(concat2)) {
                    return false;
                }
                f.a(context, jSONObject2.getString("policy"));
                concat2 = jSONObject2.optString("fileUniqueFlag");
                a3 = jSONObject2.optString("currentTime");
                int a4 = d.a(fVar.a(jSONObject2.getJSONArray("uploadInfoList"), str));
                com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "upLoadLogPut response code: " + a4);
                if (200 == a4) {
                    com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "upLoadLogPut success");
                    c.a(c.b(context), "autocheck_starttime", Long.valueOf(System.currentTimeMillis()));
                    str3 = "{url}/v2/notifyUploadSucc".replace("{url}", a2).concat("?appID=").concat(b);
                    b = a.concat("&fileUniqueFlag=").concat(concat2).concat("&uploadTime=").concat(a3);
                    com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "upload_notify_succ: " + new JSONObject(com.huawei.hianalytics.h.b.a(str3, b, a.a(str3, b, concat)).b()).getString("resCode"));
                    return true;
                }
                com.huawei.hianalytics.g.b.c("SendManagerImpl", "File upload failure");
                return false;
            }
            com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "url non conformity");
            return false;
        } catch (IOException e) {
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "sendLog get logServerUrl Exception,The Exception is IO!");
            return false;
        } catch (JSONException e2) {
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "sendLog(reauest) get logServerUrl Exception,The Exception is json!");
            return false;
        }
    }

    public boolean a(String str, String str2, String str3) {
        File[] a = a.a(str);
        if (a.length < 1) {
            com.huawei.hianalytics.g.b.d("SendManagerImpl", "There is no log file when creating a compressed package");
            return false;
        }
        File file = new File(str3 + str2);
        for (int i = 0; i < 2; i++) {
            if (f.a(a, file)) {
                com.huawei.hianalytics.log.e.a.a(new File(str));
                return true;
            }
            if (i == 1) {
                com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "Packaging failure!");
                if (file.exists() && file.delete()) {
                    com.huawei.hianalytics.g.b.b("HiAnalytics/logServer", "Bad zip file delete ok");
                }
            }
        }
        return false;
    }
}
